package servlet;

import com.mysql.jdbc.PreparedStatement;
import util.DBHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

@WebServlet("/register.do")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String NAME = request.getParameter("name");
        String TRUE_NAME = request.getParameter("true_name");
        String ADDRESS = request.getParameter("address");
        String ageStr = request.getParameter("age");
        int AGE = Integer.parseInt(ageStr == null || ageStr == "" ? "18" : ageStr);
        String SEX = request.getParameter("sex");
        String PASSWORD = request.getParameter("password");
        String invitation_code = request.getParameter("invitation_code");
        // 一段sql语句
        Connection conn = null;
        try { // 创建PreparedStatement
            conn = DBHelper.getConnection();
            String sql1 = "select name from user where name=\'"+NAME+"\'";
            String sql2 = "select name from admin where name=\'"+NAME+"\'";
            Statement stmt = conn.createStatement();
            Statement stmt1 = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql1);
            ResultSet rs2 = stmt1.executeQuery(sql2);
            if (rs.next()){
                request.setAttribute("errorMsg", "账号已存在，请重新注册！");
                request.getRequestDispatcher("register.jsp").forward(request,response);
            }else if (rs2.next()){
                request.setAttribute("errorMsg", "账号已存在，请重新注册！");
                request.getRequestDispatcher("register.jsp").forward(request,response);
            } else {
                if (!PASSWORD.equals("")){
                    if (invitation_code.equals("520")){
                        String sql = "insert into admin(name, password )values(?, ?)";
                        PreparedStatement pstat = (PreparedStatement) conn.prepareStatement(sql);
                        // PreparedStatement赋值
                        pstat.setString(1, NAME);
                        pstat.setString(2, PASSWORD);
                        // 执行
                        pstat.execute();
                        request.getSession().setAttribute("name",NAME);
                        response.sendRedirect("adminCommodity.do");
                    } else {
                        String sql = "insert into user(name, true_name,age ,sex, password,address)values(?, ?, ?, ?, ?,?)";
                        PreparedStatement pstat = (PreparedStatement) conn.prepareStatement(sql);
                        // PreparedStatement赋值
                        pstat.setString(1, NAME);
                        pstat.setString(2, TRUE_NAME);
                        pstat.setInt(3, AGE);
                        pstat.setString(4, SEX);
                        pstat.setString(5, PASSWORD);
                        pstat.setString(6, ADDRESS);
                        // 执行
                        pstat.execute();
                        request.getSession().setAttribute("name",NAME);
                        response.sendRedirect("comShow.do");
                    }
                }else {
                    request.setAttribute("errorMsg", "密码为空，请重新注册！");
                    request.getRequestDispatcher("register.jsp").forward(request,response);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
